package com.zhixing.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;

/**
 * swagger 2 word API
 *
 * @author zhixing
 */
@Slf4j
@Controller
@RequestMapping("/swagger-2-word")
public class Swagger2WordController {

    @Autowired
    private ISwagger2WordProcessor      swagger2WordProcessor;


    /**
     * Swagger 文档转后在线预览文件，可以通过右键另存为 xxx.doc 的方式另存为 word 文档
     * @param model 模板数据
     * @param url  swagger 地址
     * @return
     */
    @RequestMapping(value = "/preview-online", method = {RequestMethod.GET})
    public String getWord(Model model,
                          @RequestParam(value = "url", required = true) String url,
                          @RequestParam(value = "tags", required = false) String tags) {
        return swagger2WordProcessor.getWord(model,url,tags);
    }

    /**
     * 将 swagger 文档一键下载为 doc 文档（所有数据）
     *
     * @param model
     * @param url      需要转换成 word 文档的资源地址
     * @param response
     */
    @RequestMapping(value = "/download", method = {RequestMethod.GET})
    public void word(Model model,
                     @RequestParam(required = true) String url,
                     @RequestParam(required = true) String fileName,
                     @RequestParam(value = "tags", required = false) String tags,
                     HttpServletResponse response) {
        swagger2WordProcessor.word(model,url, fileName,tags,response);
    }
}